package com.ffyc.ssm.controller;

import com.ffyc.ssm.common.CommonResult;
import com.ffyc.ssm.modle.Ad;
import com.ffyc.ssm.modle.Admin;
import com.ffyc.ssm.modle.NewsType;
import com.ffyc.ssm.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.IntToDoubleFunction;

@Transactional
@RestController
@RequestMapping(path = "/newsType")
public class NewsTypeController {

    @Autowired
    private NewsTypeService service;

    @GetMapping(path = "/list")
    public CommonResult showAdList(NewsType newsType){
        CommonResult<List> commonResult = null;
        try {
            List<Ad> list = service.findList(newsType);
            if (list!=null){
                commonResult = new CommonResult<>(200,"查询成功" , list);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    @GetMapping(path = "/findNewsType")
    public CommonResult findNewsType(Integer id){
        CommonResult commonResult = null;
        try {
            NewsType newsType = service.findNewsType(id);
            commonResult = new CommonResult<>(200,"查询成功" , newsType);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    @PostMapping(path = "/save")
    public CommonResult save(@RequestParam("name")String name,@RequestParam("id")String id){
        CommonResult commonResult = null;
        try {
            Map<String,String> map = new HashMap();
            map.put("name",name);
            map.put("id",id);
            service.save(map);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }



    @PostMapping(path = "/update")
    public CommonResult update(@RequestParam("name")String name,@RequestParam("adminId")String adminId,@RequestParam("id")String id){
        CommonResult commonResult = null;
        try {
            Map<String,String> map = new HashMap();
            map.put("name",name);
            map.put("adminId",adminId);
            map.put("id",id);
            service.update(map);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    @PostMapping(path = "/delete")
    public CommonResult delete(Integer id){
        CommonResult commonResult = null;
        try {
            service.delete(id);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }


}
